import {
    observable,
    action
} from "mobx";
import axios from '../../utils/http';
import * as api from '../../utils/api'
import {
    message
} from "antd";




class ChouJiang {

    @observable
    myFrom = {
        keyword: '',
        groudNum: 0, //团购人数
        ludrPrice: 0, //幸运抽奖价格
        ludrNum: 0, //参加幸运抽奖人数
        ludrDate: [], //幸运抽奖时间
        ludrRule: '', //抽奖规则
        goodsId: null, //商品id
        inUpdate:false
    }

    @observable //商品List
    goodslist = []

    @observable //表格Data
    tabDataAll = []

    @observable //详情List
    xiangQingData = []

    @observable//是否已经抽奖
    isZhongJiang=null


    @action//双向绑定
    insertStart = (data, setData) => {
        console.log(setData)
        if (data === 'keyword') this.myFrom.keyword = setData.target.value //修改关键字
        else if (data === 'groudNum') this.myFrom.groudNum = setData.target.value //修改人数
        else if (data === 'ludrPrice') this.myFrom.ludrPrice = setData.target.value //修改幸运抽奖价格
        else if (data === 'ludrNum') this.myFrom.ludrNum = setData.target.value //修改参加幸运抽奖人数
        else if (data === 'ludrDate') this.myFrom.ludrDate = setData //修改时间
        else if (data === 'goodsId') this.myFrom.goodsId = setData //修改商品名称
        else if (data === 'ludrRule') this.myFrom.ludrRule = setData.target.value //修改秒杀规则
        else if (data === 'update') this.myFrom = {
            ludrId: setData.ludrId,
            goodsId: setData.goods.goodsId,
            groudNum: setData.groudNum,
            goodsName:setData.goods.goodsName,
            ludrPrice: setData.ludrPrice,
            ludrNum: setData.ludrNum,
            ludrRule: setData.ludrRule,
            ludrDate: [setData.ludrStart, setData.ludrStart],
            inUpdate: true
        }
        else if(data == 'insert') this.myFrom = {
            keyword: '',
            groudNum: 0, //团购人数
            ludrPrice: 0, //幸运抽奖价格
            ludrNum: 0, //参加幸运抽奖人数
            ludrDate: [], //幸运抽奖时间
            ludrRule: '', //抽奖规则
            goodsId: null, //商品id
            inUpdate:false
        }
        else if(data === 'GoodsId') this.tuanGouStatusNum = setData
    }

    @action //初始化数据
    insertTabData() {
        return axios({
            url: api.GET_CHOUJIANG_TAB,
            method: 'get'
        }).then(data => {
            return data.data.data
        })
    }

    @action //根据ID删除数据
    delTabData(id) {
        return axios({
            url: api.DEL_ID_CHOUJIANG,
            method: 'get',
            params: {
                ludrId: id
            }
        }).then(data => {
            console.log(data)
            if (data.data.code === 200) {
                return data.data.data
            } else {
                message.error('出错了，请稍后再试')
            }
        })
    }

    @action //根据ID查询
    selXiangQing(id) {
        return axios({
            url: api.GET_STUTES_TUANGOU,
            method: 'get',
            params: {
                bulkStatus:'',
                bulkOid:''
            }
        }).then(data => {
            console.log(data)
            if (data.data.code === 200) {
                this.xiangQingData = data.data.data.map(item=>{
                    item.goodsId = item.bulkId
                    item.goodsName = item.bulkOid
                    return item
                })
                return 200
            } else {
                return data.data
            }
        })
    }

    @action //查商品
    selGoods() {
        console.log();
        axios({
            url: api.GET_ALL_SHANGPIN,
            method: 'get'
        }).then(data => {
            console.log(data)
            this.goodslist = data.data.data
        })
    }

    @action //增加或修改幸运抽奖信息
    insertTab() {
        let ludrStart = new Date(this.myFrom.ludrDate[0])
        let ludrLast = new Date(this.myFrom.ludrDate[1])
        if (this.myFrom.inUpdate) {
            return axios({
                url: api.UPDATE_TAB,
                method: 'post',
                data: {
                    ludrId:this.myFrom.ludrId,
                    goodsId: this.myFrom.goodsId,
                    groudNum: this.myFrom.groudNum,
                    ludrPrice: this.myFrom.ludrPrice,
                    ludrNum: this.myFrom.ludrNum,
                    ludrRule: this.myFrom.ludrRule,
                    ludrStart,
                    ludrStart
                }
            }).then(data => {
                if (data.data.code === 200) {
                    message.success('修改成功')
                    return 200
                } else {
                    return 400
                }
            })
        } else {
            return axios({
                url: api.ADD_CHOUJIANG,
                method: 'post',
                data: {
                    goodsId: this.myFrom.goodsId,
                    groudNum: this.myFrom.groudNum,
                    ludrPrice: this.myFrom.ludrPrice,
                    ludrNum: this.myFrom.ludrNum,
                    ludrRule: this.myFrom.ludrRule,
                    ludrStart,
                    ludrStart
                }
            }).then(data => {
                if (data.data.code === 200) {
                    message.success('添加成功')
                    return 200
                } else {
                    return 400
                }
            })

        }




    }

    @observable
    tuanGouStatusNum = ''

    @action//根据团购状态查询团购信息
    updateStutes(id){
        let iid
            for(let i = 0;i < this.xiangQingData.length; i ++){
                console.log(this.xiangQingData[i].goodsId,this.tuanGouStatusNum,this.xiangQingData[i].goodsName)
                if(this.xiangQingData[i].goodsId === this.tuanGouStatusNum)  iid = this.xiangQingData[i].goodsName
            }
            
        console.log(iid)
        axios({
            url:api.GET_STUTES_TUANGOU,
            method:'get',
            params:{
                bulkStatus:id,
                bulkOid:iid
            }
        }).then(data=>{
            console.log(data)
            this.xiangQingData = data.data.data.map(item=>{
                item.goodsId = item.bulkId
                item.key = item.bulkId
                item.goodsName = item.bulkOid
                return item
            })
        })
    }

    @action
    updateNum(id){

    }

    @action//查询是否已抽奖
    async getZhongJiang(id){
        
        let dataGoods = await axios({
            url:api.GET_XINGYUNCHOUJIANG,
            method:'get',
            params:{
                ludrId:id
            }
        }).then(data=>{
            console.log(data)
            return  data.data.data 
        })
        if(dataGoods.msg === "活动已经结束"){
            await axios({
                url:api.DJL_GET_TUANGOUZHUANGTAI,
                method:'get',
                params:{
                    ludrId:id,
                    goodsId:dataGoods.data.goods.goodsId
                }
            }).then(data=>{
                console.log(data)
            })

            await axios({
                url:api.GET_ISCHOUJIAN,
                method:'get',
                params:{
                    goodsId:dataGoods.data.goods.goodsId
                }
            }).then(data=>{
                console.log(data)
            })
        }else{
            message.warning(dataGoods.msg)
        }
    }
}

export default ChouJiang